<?php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'mobile','sign', 'password','avatar','coins','is_vip','datanum','vipduetime','upmoney','downcoins','money','realname','alipay_account','is_active','tixianmoney','qqname','qqopenid','wechatname','wechatopenid'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    protected $dates = ['vipduetime'];
    public function getMonthDayAttribute($date)
    {
        return $date->format('m-d');
    }

    public function findForPassport($username)
    {
        return \App\User::normal()
            ->where('mobile', $username)
            ->first();
    }



    public function scopeNormal($query)
    {
        return $query->where('is_active', 1);
    }

    public function datas(){
        return $this->hasMany(Data::class);
    }
    public function collects(){
        return $this->hasMany(Collect::class);
    }
    public function downs(){
        return $this->hasMany(Earning::class);
    }
    public function earnings(){
        return $this->hasMany(Earning::class,'earning_user_id','id');
    }
    public function tixians(){
        return $this->hasMany(Tixian::class);
    }
    public function invitations(){
        return $this->hasMany(Invitation::class,'invite_user_id','id');
    }
    //用户金额获取
    public function getMoneyAttribute($value)
    {
        return $value/100;
    }
    //用户金额修改
    public function setMoneyAttribute($value)
    {
        $this->attributes['money'] = $value*100;
    }
    //提现金额获取
    public function getTixianmoneyAttribute($value)
    {
        return $value/100;
    }
    //提现金额修改器
    public function setTixianmoneyAttribute($value)
    {
        $this->attributes['tixianmoney'] = $value*100;
    }
    //累计充值获取
    public function getUpmoneyAttribute($value)
    {
        return $value/100;
    }
    //累计充值修改
//    public function setUpmoneyAttribute($value)
//    {
//        $this->attributes['upmoney'] = $value * 100;
//    }
    public function validateForPassportPasswordGrant($password)
    {
        return $password == $this->password || Hash::check($password, $this->password);
    }
}
